package com.leoman.controller.backend;

import com.leoman.core.Constant;
import com.leoman.entity.AdminUser;
import com.leoman.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 总管理后台控制面板
 * Created by 涂奕恒 on 2016/10/20.
 */
@Controller
@RequestMapping(value = "backend")
public class BackendIndexController {

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private CompanyUserService companyUserService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private PostService postService;

    @Autowired
    private FeedBackService feedBackService;

    @RequestMapping(value = "/dashboard")
    public String dashboard(HttpServletRequest request,
                            Integer index,
                            ModelMap model) {
        if (null != index) {
            model.addAttribute("index", index);
        }

        // 获取当前登录人信息
        AdminUser adminUser = (AdminUser) request.getSession().getAttribute(Constant.CURRENT_ADMIN_USER);

        if (null == adminUser) {
            return "redirect:/login";
        }

        model.addAttribute("adminUser", adminUser);

        // 查询新增普通用户数
        Integer newAppUserCount = appUserService.findCountWithNewAppUser(index);
        model.addAttribute("newAppUserCount", newAppUserCount);

        // 查询新增企业用户数
        Integer newCompanyUserCount = companyUserService.findCountWithNewCompanyUser(index);
        model.addAttribute("newCompanyUserCount", newCompanyUserCount);

        // 查询新增视频数
        Integer newVideoCount = videoService.findCountWithNewVideo(index);
        model.addAttribute("newVideoCount", newVideoCount);

        // 查询新增帖子数
        Integer newPostCount = postService.findCountWithNewPost(index);
        model.addAttribute("newPostCount", newPostCount);

        // 查询新增申请认证企业用户数
        Integer newCompanyUserCountNoCheck = companyUserService.findCountWithNewCompanyUserNoCheck(index);
        model.addAttribute("newCompanyUserCountNoCheck", newCompanyUserCountNoCheck);

        // 查询新增普通用户提现申请数
        Integer newAppUserGainCount = appUserService.findNewAppUserGainCount(index);
        model.addAttribute("newAppUserGainCount", newAppUserGainCount);

        // 查询新增意见反馈数
        Integer newFeedBackCount = feedBackService.findCountWithNewAdd(index);
        model.addAttribute("newFeedBackCount", newFeedBackCount);


        /**
         * 视频统计信息
         */
        Integer allVideoCount = videoService.findAll().size();
        model.addAttribute("allVideoCount", allVideoCount);

        Integer waitCheckVideoCount = videoService.findListByCheckStatus(Constant.CHECK_STATUS_DEFAULT).size();
        model.addAttribute("waitCheckVideoCount", waitCheckVideoCount);

        Integer workVideoCount = videoService.findListByRecordStatus(Constant.CHECK_STATUS_SUCCESS).size();
        model.addAttribute("workVideoCount", workVideoCount);

        Integer noShoppingVideoCount = videoService.findListByIsShopping(Constant.VIDEO_SHOPPING_STATUS_NO).size();
        model.addAttribute("noShoppingVideoCount", noShoppingVideoCount);

        Integer failCheckVideoCount = videoService.findListByCheckStatus(Constant.CHECK_STATUS_FAIL).size();
        model.addAttribute("failCheckVideoCount", failCheckVideoCount);


        /**
         * 用户帖子统计信息
         */
        Integer allPostCount = postService.findAll().size();
        model.addAttribute("allPostCount", allPostCount);

        Integer waitCheckPostCount = postService.findListByCheckStatus(Constant.CHECK_STATUS_DEFAULT).size();
        model.addAttribute("waitCheckPostCount", waitCheckPostCount);

        Integer okCheckPostCount = postService.findListByCheckStatus(Constant.CHECK_STATUS_SUCCESS).size();
        model.addAttribute("okCheckPostCount", okCheckPostCount);


        /**
         * 用户统计信息
         */
        Integer allAppUserCount = appUserService.findAll().size();
        model.addAttribute("allAppUserCount", allAppUserCount);

        Integer allCompanyUserCount = companyUserService.findAll().size();
        model.addAttribute("allCompanyUserCount", allCompanyUserCount);

        Integer allUserCount = allAppUserCount + allCompanyUserCount;
        model.addAttribute("allUserCount", allUserCount);

        return "backend/控制面板";
    }
}
